﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using VIP.Extension;

namespace VIP
{

    public class VIPRequest : VIPMonoSingleton<VIPRequest>
    {

        /// <summary>
        /// 根链接
        /// </summary>
        public string Api = "http://192.168.0.125:8068/api/";

        /// <summary>
        /// Token键
        /// </summary>
        private readonly string TokenKey = "Authorization";

        /// <summary>
        /// Token值
        /// </summary>
        public static string Token;

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url"></param>
        public void GetRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> Header = null)
        {
            StartCoroutine(IeGetRequest(Api + url, OnCallback, Header));
        }

        /// <summary>
        /// Get请求(携程)
        /// </summary>
        /// <param name="url"></param>
        private IEnumerator IeGetRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> Header)
        {
            UnityWebRequest webRequest = UnityWebRequest.Get(url);
            if (Token.IsNotEmpty())
            {
                webRequest.SetRequestHeader(TokenKey, Token);
            }
            //添加头部内容
            if (Header.IsNotNull()) {
                foreach (KeyValuePair<string, string> kvp in Header)
                {
                    webRequest.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                string text = webRequest.error;
                ErrorHit(webRequest.responseCode);
            }
            else
            {
                string text = webRequest.downloadHandler.text;
                VIPData vipData = VIPData.ToVIPData(text);
                if (OnCallback.IsNotNull())
                {
                    OnCallback(vipData);
                }
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        public void PostRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> formFields = null, Dictionary<string, string> Header = null)
        {
            StartCoroutine(IePostRequest(Api + url, OnCallback, formFields, Header));
        }

        /// <summary>
        /// Post请求
        /// </summary>
        public void PostRequest(string url, UnityAction<VIPData> OnCallback, string formFields = null, Dictionary<string, string> Header = null)
        {
            StartCoroutine(IePostRequest(Api + url, OnCallback, formFields, Header));
        }

        /// <summary>
        /// Post请求(携程)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="formFields"></param>
        private IEnumerator IePostRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> formFields, Dictionary<string, string> Header)
        {
            UnityWebRequest webRequest = UnityWebRequest.Post(url, formFields);
            if (Token.IsNotEmpty())
            {
                webRequest.SetRequestHeader(TokenKey, Token);
            }
            //添加头部内容
            if (Header.IsNotNull())
            {
                foreach (KeyValuePair<string, string> kvp in Header)
                {
                    webRequest.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                string text = webRequest.error;
                ErrorHit(webRequest.responseCode);
            }
            else
            {
                string text = webRequest.downloadHandler.text;
                VIPData vipData = VIPData.ToVIPData(text);
                if (OnCallback.IsNotNull())
                {
                    OnCallback(vipData);
                }
            }
        }

        /// <summary>
        /// Post请求(携程)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="formFields"></param>
        private IEnumerator IePostRequest(string url, UnityAction<VIPData> OnCallback, string formFields, Dictionary<string, string> Header)
        {
            UnityWebRequest webRequest = UnityWebRequest.Post(url, formFields);
            DownloadHandlerBuffer dhb = new DownloadHandlerBuffer();
            byte[] body = Encoding.UTF8.GetBytes(formFields);
            webRequest.uploadHandler = new UploadHandlerRaw(body);
            webRequest.downloadHandler = dhb;
            if (Token.IsNotEmpty())
            {
                webRequest.SetRequestHeader(TokenKey, Token);
            }
            //添加头部内容
            if (Header.IsNotNull())
            {
                foreach (KeyValuePair<string, string> kvp in Header)
                {
                    webRequest.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                string text = webRequest.error;
                ErrorHit(webRequest.responseCode);
            }
            else
            {
                string text = webRequest.downloadHandler.text;
                VIPData vipData = VIPData.ToVIPData(text);
                if (OnCallback.IsNotNull())
                {
                    OnCallback(vipData);
                }
            }
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bodyData"></param>
        /// <param name="OnCallback"></param>
        public void PutRequest(string url, UnityAction<VIPData> OnCallback, string bodyData = null, Dictionary<string, string> Header = null)
        {
            StartCoroutine(IePutRequest(Api + url, OnCallback, bodyData, Header));
        }

        /// <summary>
        /// Put请求(携程)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bodyData"></param>
        /// <returns></returns>
        public IEnumerator IePutRequest(string url, UnityAction<VIPData> OnCallback, string bodyData, Dictionary<string, string> Header)
        {
            UnityWebRequest webRequest = UnityWebRequest.Put(url, bodyData);
            if (Token.IsNotEmpty())
            {
                webRequest.SetRequestHeader(TokenKey, Token);
            }
            //添加头部内容
            if (Header.IsNotNull())
            {
                foreach (KeyValuePair<string, string> kvp in Header)
                {
                    webRequest.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                string text = webRequest.error;
                ErrorHit(webRequest.responseCode);
            }
            else
            {
                string text = webRequest.downloadHandler.text;
                VIPData vipData = VIPData.ToVIPData(text);
                if (OnCallback.IsNotNull())
                {
                    OnCallback(vipData);
                }
            }
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bodyData"></param>
        /// <param name="OnCallback"></param>
        public void DeleteRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> Header = null)
        {
            StartCoroutine(IeDeleteRequest(Api + url, OnCallback, Header));
        }

        /// <summary>
        /// Delete请求（携程）
        /// </summary>
        /// <returns></returns>
        public IEnumerator IeDeleteRequest(string url, UnityAction<VIPData> OnCallback, Dictionary<string, string> Header)
        {
            UnityWebRequest webRequest = UnityWebRequest.Delete(url);
            if (Token.IsNotEmpty())
            {
                webRequest.SetRequestHeader(TokenKey, Token);
            }
            //添加头部内容
            if (Header.IsNotNull())
            {
                foreach (KeyValuePair<string, string> kvp in Header)
                {
                    webRequest.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError)
            {
                string text = webRequest.error;
                ErrorHit(webRequest.responseCode);
            }
            else
            {
                string text = webRequest.downloadHandler.text;
                VIPData vipData = VIPData.ToVIPData(text);
                if (OnCallback.IsNotNull())
                {
                    OnCallback(vipData);
                }
            }
        }

        /// <summary>
        /// 根据状态码提示错误
        /// </summary>
        /// <param name="responseCode"></param>
        private void ErrorHit(long responseCode)
        {
            switch (responseCode)
            {
                case 0:
                    {
                        VIPToast.Instance.Show("请求超时");
                    }
                    break;
                case 1001:
                    {
                        VIPToast.Instance.Show("未登陆");
                    }; break;
                default:
                    {
                        VIPToast.Instance.Show("未知错误：" + responseCode);
                    }
                    break;
            }
        }

    }

}
